package com.huhu.scenes;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.util.GLState;

import android.content.res.Resources.Theme;
import android.location.Address;
import android.text.Editable;
import android.text.method.KeyListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import com.huhu.GameActivity;
import com.huhu.base.BaseScene;
import com.huhu.manager.SceneManager;
import com.huhu.manager.SceneManager.SceneType;

public class MainMenuScene extends BaseScene implements
		IOnMenuItemClickListener{

	private MenuScene menuChildScene;
	private static final int MENU_PLAY = 0;
	private static final int MENU_OPTIONS = 1;
	private static final int MENU_ABOUT = 2;
	private static final int MENU_BACK = 3;
	private static final int MENU_SOUND = 4;
	private static final int MENU_MUSIC = 5;
	private int i = 0;
	
	private MenuScene menuOptionsScene;
	private MenuScene menuAboutScene;

	@Override
	public void createScene() {
		createMenuBackground();
		createMenuChildScene();
		
		createOptionsMenu();
		createAboutMenu();
	}

	@Override
	public void onBackKeyPressed() {
		System.exit(0);

	}

	@Override
	public SceneType getSceneType() {
		// TODO Auto-generated method stub
		return SceneType.MENU;
	}

	@Override
	public void disposeScene() {
		// TODO Auto-generated method stub

	}

	private void createMenuBackground() {
		Sprite menuBackgroundSprite = new Sprite(0, 0, resourcesManager.menu_background_region,
				vbom) {
			@Override
			protected void preDraw(GLState pGLState, Camera pCamera) {
				// TODO Auto-generated method stub
				super.preDraw(pGLState, pCamera);
				pGLState.enableDither();
			}
		};
		menuBackgroundSprite.setScale((2f));
		attachChild(menuBackgroundSprite);
	}

	private void createMenuChildScene() {
		menuChildScene = new MenuScene(camera);
		final Sprite mSpriteFrame = new Sprite(0, 0,
				resourcesManager.menu_frame_region, vbom);
		mSpriteFrame.setPosition(GameActivity.CAMERA_WIDTH/2 - mSpriteFrame.getWidth() / 2,
				GameActivity.CAMERA_HEIGHT/2 - mSpriteFrame.getHeight() / 2);
		menuChildScene.attachChild(mSpriteFrame);
		
		final Text strangeText = new Text(0, 0, resourcesManager.font, "STRANGE ADVENTURE", vbom);
		strangeText.setPosition(220, 150);
		menuChildScene.attachChild(strangeText);
		
		final Sprite mSpriteSaveFrame = new Sprite(0, 0,
				resourcesManager.menu_save_frame_region, vbom);
		mSpriteSaveFrame.setPosition(170, 210);
		menuChildScene.attachChild(mSpriteSaveFrame);
		
		Sprite mSpriteSaveHer = new Sprite(0, 0, resourcesManager.menu_save_her_region, vbom);
		mSpriteSaveHer.setPosition(215, 270);
		menuChildScene.attachChild(mSpriteSaveHer);

		final IMenuItem playMenuItem = new ScaleMenuItemDecorator(
				new SpriteMenuItem(MENU_PLAY,
						resourcesManager.menu_start_region, vbom), 1.2f, 1);
		final IMenuItem optionsMenuItem = new ScaleMenuItemDecorator(
				new SpriteMenuItem(MENU_OPTIONS,
						resourcesManager.menu_options_region, vbom), 1.2f, 1);
		final IMenuItem aboutMenuItem = new ScaleMenuItemDecorator(
				new SpriteMenuItem(MENU_ABOUT,
						resourcesManager.menu_about_region, vbom), 1, 1.2f);

		menuChildScene.addMenuItem(playMenuItem);
		menuChildScene.addMenuItem(optionsMenuItem);
		menuChildScene.addMenuItem(aboutMenuItem);

		menuChildScene.buildAnimations();
		menuChildScene.setBackgroundEnabled(false);

		playMenuItem.setPosition(playMenuItem.getX() + 90,
				playMenuItem.getY() + 100);
		optionsMenuItem.setPosition(optionsMenuItem.getX()-35,
				optionsMenuItem.getY() + 100);
		aboutMenuItem.setPosition(aboutMenuItem.getX() + 195,
				optionsMenuItem.getY());
		menuChildScene.setOnMenuItemClickListener(this);
		this.setChildScene(menuChildScene);
	}
	
	private void createOptionsMenu(){
		menuOptionsScene = new MenuScene(camera);
		final Sprite mSpriteFrame = new Sprite(0, 0,
				resourcesManager.menu_frame_region, vbom);
		mSpriteFrame.setPosition(GameActivity.CAMERA_WIDTH/2 - mSpriteFrame.getWidth() / 2,
				GameActivity.CAMERA_HEIGHT/2 - mSpriteFrame.getHeight() / 2);
		menuOptionsScene.attachChild(mSpriteFrame);
		
		final Sprite mSpriteSaveFrame = new Sprite(0, 0,
				resourcesManager.menu_save_frame_region, vbom);
		mSpriteSaveFrame.setPosition(170, 210);
		menuOptionsScene.attachChild(mSpriteSaveFrame);
		
		final Sprite mSpriteOptionsGear = new Sprite(0, 0,
				resourcesManager.menu_options_gear_region, vbom);
		mSpriteOptionsGear.setPosition(215, 270);
		menuOptionsScene.attachChild(mSpriteOptionsGear);
		
		final Text soundText = new Text(480, 220, resourcesManager.font, "Sound", vbom);
		final Text musicText = new Text(480, 320, resourcesManager.font, "Music", vbom);
		menuOptionsScene.attachChild(soundText);
		menuOptionsScene.attachChild(musicText);
		
		final IMenuItem soundMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_SOUND, resourcesManager.menu_sound_region, vbom), 1, 1.2f);
		final IMenuItem musicMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_MUSIC, resourcesManager.menu_music_region, vbom), 1, 1.2f);
		final IMenuItem backMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_BACK, resourcesManager.menu_back_region, vbom), 1, 1.2f);
		
		menuOptionsScene.addMenuItem(soundMenuItem);
		menuOptionsScene.addMenuItem(musicMenuItem);
		menuOptionsScene.addMenuItem(backMenuItem);
		
		menuOptionsScene.buildAnimations();
		menuOptionsScene.setBackgroundEnabled(false);
		

		soundMenuItem.setPosition(680, 220);
		musicMenuItem.setPosition(680, 320);
		backMenuItem.setPosition(550, 400);
		
		menuOptionsScene.setOnMenuItemClickListener(this);
	}
	
	private void createAboutMenu(){
		menuAboutScene = new MenuScene(camera);
		final Sprite mSpriteFrame = new Sprite(0, 0,
				resourcesManager.menu_frame_region, vbom);
		mSpriteFrame.setPosition(GameActivity.CAMERA_WIDTH/2 - mSpriteFrame.getWidth() / 2,
				GameActivity.CAMERA_HEIGHT/2 - mSpriteFrame.getHeight() / 2);
		menuAboutScene.attachChild(mSpriteFrame);
		
		final Sprite mSpriteSaveFrame = new Sprite(0, 0,
				resourcesManager.menu_save_frame_region, vbom);
		mSpriteSaveFrame.setPosition(170, 210);
		menuAboutScene.attachChild(mSpriteSaveFrame);
		
		final Sprite mSpriteAboutLogo = new Sprite(0, 0, resourcesManager.menu_about_logo_region, vbom);
		mSpriteAboutLogo.setPosition(210, 270);
		menuAboutScene.attachChild(mSpriteAboutLogo);
		
		final IMenuItem backMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_BACK, resourcesManager.menu_back_region, vbom), 1, 1.2f);
		menuAboutScene.addMenuItem(backMenuItem);
		
		menuAboutScene.buildAnimations();
		menuAboutScene.setBackgroundEnabled(false);
		
		backMenuItem.setPosition(550, 400);
		
		menuAboutScene.setOnMenuItemClickListener(this);
	}
	

	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {
		switch (pMenuItem.getID()) {
		case MENU_PLAY:
			SceneManager.getInstance().createLevelScene();
			break;
		case MENU_OPTIONS:
			this.clearChildScene();
			this.setChildScene(menuOptionsScene);
			break;
		case MENU_ABOUT:
			this.clearChildScene();
			this.setChildScene(menuAboutScene);
			break;
		case MENU_BACK:
			this.clearChildScene();
			this.setChildScene(menuChildScene);
			break;
		case MENU_SOUND:
			
			break;
		case MENU_MUSIC:
			
			break;
		}
		return true;
	}
}
